package cn.kpi.service.sys.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.kpi.dao.sys.OrgDao;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.service.sys.IOrgService;

@Service
public class OrgServiceImpl implements IOrgService {
	
	@Autowired
	private OrgDao orgDao;
	
	public int InsertEntity(OrgEntity entity) throws Exception{
		try{
			Map<String, Object> map = new HashMap<String, Object>();
		    map.put("morgid", entity.getOrgparentid());
		    map.put("morgname", entity.getOrgname());
		    map.put("mremark", entity.getRemark());
		    map.put("morgcode", entity.getOrgcode());
		    map.put("morgtype", entity.getOrgtype());
		    map.put("morderindex", entity.getOrderindex());
		    map.put("mresult", 0);
		    orgDao.InsertEntity(map);
		    Object result=map.get("mresult");
		    //System.out.println(result);
		    return Integer.parseInt(String.valueOf(result));
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	public int UpdateEntity(OrgEntity entity)throws Exception{
		try{
			return orgDao.UpdateEntity(entity);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	
	public int DeleteEntity(Long orgid) throws Exception{
		try{
			Map<String, Object> map = new HashMap<String, Object>();
		    map.put("morgid", orgid);
		    map.put("mresult", 0);
		    orgDao.DeleteEntity(map);
		    Object result=map.get("mresult");
		    //System.out.println(result);
		    return Integer.parseInt(String.valueOf(result));
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	
	public List<OrgEntity> GetOrgList()throws Exception{
		try{
		    return orgDao.GetOrgList();
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	public OrgEntity GetOrgById(Long orgid)throws Exception{
		try{
		    return orgDao.GetOrgById(orgid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public OrgEntity GetOrgByName(String orgName) throws Exception {
		try{
			return orgDao.GetOrgByName(orgName);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	public List<OrgEntity> GetOrgListByWhere(String where)throws Exception{
		try{
		    return orgDao.GetOrgListByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
}
